package main

import (
	"fmt"
)

//切片是引用类型
func main() {

	operationSlice()
	return

	arr := [...]int{0,1,2,3,4,5,6}

	//s1 := arr[2:6]
	//s2 := s1[3:5]
	//fmt.Println("s1 = ", s1)
	//fmt.Println("s2 = ", s2)  //结果5,6  切片向后扩展，可以找到初始变量的cap

	//return

	s := arr[2:6]
	fmt.Println("arr[2:6] = ", s)
	fmt.Println("arr[:6] =", arr[:6])
	s1 := arr[2:]
	fmt.Println("s1 = ", s1)
	s2 := arr[:]
	fmt.Println("s2 = ", s2)
	s3 := append(s2,100)
	fmt.Println("s3 = ", s3)
	fmt.Println("arr = ", arr)
	fmt.Println("After updateSlice(s1)")
	updateSlice(s1)
	fmt.Println(s1)
	fmt.Println(arr)

	fmt.Println("After updateSlice(s2)")
	updateSlice(s2)
	fmt.Println(s2)
	fmt.Println(arr)

	fmt.Println("Reslice")
	s2 = s2[:5]
	fmt.Println(s2)
	s2 = s2[2:]
	fmt.Println(s2)
}

func updateSlice(s []int) {
	s[0] = 100
}

func operationSlice()  {
	var s []int

	for i := 0; i < 100; i++ {
		printSlice(s)
		s = append(s, 2 * i +1)
	}
	s1 := []int{2,3,4,5}
	printSlice(s1)
	s2 := make([]int,16)
	printSlice(s2)
	s3 := make([]int,16,30)
	printSlice(s3)

	fmt.Println("copy slice")
	s4 := make([]int,1)
	copy(s3, s4)
	printSlice(s4)
	fmt.Println("delete slice")
	s1 = append(s1[:1], s1[2:]...)
	fmt.Println("s1 = ", s1)
	fmt.Println("popping from back")
	fmt.Println("s1 = ", s1)
	tail := s1[len(s1) - 1]
	s1 = s1[:len(s1)-1]
	fmt.Println("tail = ", tail)
	fmt.Println("s1 = ", s1)


}

func printSlice(s []int)  {
	fmt.Printf("len = %d, cap = %d\n", len(s), cap(s))
}


